package com.cykj.pos.service.impl;

import com.cykj.pos.domain.BizAdvertisingType;
import com.cykj.pos.mapper.BizAdvertisingTypeMapper;
import com.cykj.pos.profit.dto.BizAdvertisingDTO;
import com.cykj.pos.util.TencentCOSUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.BizAdvertisingMapper;
import com.cykj.pos.domain.BizAdvertising;
import com.cykj.pos.service.IBizAdvertisingService;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 广告Service业务层处理
 *
 * @author weijianbo
 * @date 2021-04-26
 */
@Service
public class BizAdvertisingServiceImpl extends ServiceImpl<BizAdvertisingMapper, BizAdvertising> implements IBizAdvertisingService {

    @Resource
    private BizAdvertisingMapper bizAdvertisingMapper;

    @Resource
    private BizAdvertisingTypeMapper bizAdvertisingTypeMapper;

    @Override
    public List<BizAdvertising> queryList(BizAdvertising bizAdvertising) {
        LambdaQueryWrapper<BizAdvertising> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(bizAdvertising.getAdvTitle())){
            lqw.eq(BizAdvertising::getAdvTitle ,bizAdvertising.getAdvTitle());
        }
        if (StringUtils.isNotBlank(bizAdvertising.getAdvPicture())){
            lqw.eq(BizAdvertising::getAdvPicture ,bizAdvertising.getAdvPicture());
        }
        if (StringUtils.isNotBlank(bizAdvertising.getAdvJump())){
            lqw.eq(BizAdvertising::getAdvJump ,bizAdvertising.getAdvJump());
        }
        if (bizAdvertising.getType() != null){
            lqw.eq(BizAdvertising::getType ,bizAdvertising.getType());
        }
        if (bizAdvertising.getAdvNewsId() != null){
            lqw.eq(BizAdvertising::getAdvNewsId ,bizAdvertising.getAdvNewsId());
        }
        if (bizAdvertising.getSort() != null){
            lqw.eq(BizAdvertising::getSort ,bizAdvertising.getSort());
        }
        if (bizAdvertising.getForbidden() != null){
            lqw.eq(BizAdvertising::getForbidden ,bizAdvertising.getForbidden());
        }
        return this.list(lqw);
    }

    @Override
    public void saveBizAdvertising(BizAdvertising bizAdvertising) {
        
        String[] strsplit = bizAdvertising.getAdvPicture().split("/");
        System.out.println(strsplit);
        String url = TencentCOSUtils.SimpleUploadFileFromLocal("advertising/"+strsplit[strsplit.length-1],"F:\\picture\\pp.jpg");
        bizAdvertising.setAdvPicture(url);
        int row = bizAdvertisingMapper.saveBizAdvertising(bizAdvertising);
    }

    @Override
    public List<BizAdvertisingDTO> selectAdvertisingList(BizAdvertisingType bizAdvertisingType) {
        // 按照类型分广告位
        List<BizAdvertisingDTO> listByType = bizAdvertisingMapper.selectAdvertisingList(bizAdvertisingType);

        return listByType;
    }
}
